<template>
	<view class="home-container">
		<!-- Tab切换栏 -->
		<view class="tab-bar">
			<view class="tab-item" :class="{ active: activeTab === 'passenger' }" @click="switchTab('passenger')">我是乘客</view>
			<view class="tab-item" :class="{ active: activeTab === 'motorists' }" @click="switchTab('motorists')">我是车主</view>
		</view>

		<view class="container">
			<!-- 行程列表 -->
			<view class="trip-list" v-if="activeTab === 'passenger'">
				<view v-if="trips.length > 0" class="trip-item" v-for="(item, index) in trips" :key="index" @click="navigateTo(item)">
					<view class="trip-item-detail" v-if="activeTab === 'passenger'">
						<view class="departure-info">
							<view class="item_info">
								<text class="item_label">{{ item.start_name }}</text>
								<text class="item_text">-></text>
								<text class="item_label">{{ item.end_name }}</text>
							</view>
							<view class="item_info">
								<text class="item_label">时间:</text>
								<text class="item_text">{{ item.start_time }}</text>
							</view>
							<view class="item_info">
								<text class="item_label">车主:</text>
								<text class="item_text">（行程结束,电话隐藏）</text>
							</view>
							<view class="item_info">
								<text class="item_label">人数:</text>
								<text class="item_text">{{ item.passenger_count || 1 }}人</text>
							</view>
							<view class="item_info">
								<text class="item_label">上车点:</text>
								<text class="item_text">{{ item.boarding_location }}</text>
							</view>
						</view>
						<view class="action-buttons">
							<view class="view-btn">查看</view>
							<view class="status-text">{{ statusText[item.status] }}</view>
						</view>
					</view>
				</view>
				<empty-data v-if="trips.length == 0" defaultText="暂无行程数据" />
			</view>

			<view class="trip-list" v-if="activeTab === 'motorists'">
				<view v-if="trips.length > 0" class="trip-item" v-for="(item, index) in trips" :key="index" @click="navigateTo(item)">
					<view class="trip-item-detail" v-if="activeTab === 'motorists'">
						<view class="departure-info">
							<view class="item_info">
								<text class="item_label">{{ (item.start && item.start.name) || item.start_name || '起点' }}</text>
								<text class="item_text">-></text>
								<text class="item_label">{{ (item.end && item.end.name) || item.end_name || '终点' }}</text>
							</view>
							<view class="item_info">
								<text class="item_label">时间:</text>
								<text class="item_text">{{ item.start_time }}</text>
							</view>
							<view class="item_info">
								<text class="item_label">订座/余座:</text>
								<text class="item_text" style="text-decoration: underline">{{ item.on_order }}人/{{ item.current_seats }}人</text>
							</view>
						</view>
						<view class="action-buttons">
							<view class="view-btn">查看</view>
							<view class="status-text">{{ statusText[item.status] }}</view>
						</view>
					</view>
				</view>
				<empty-data v-if="trips.length == 0" defaultText="暂无行程数据" />
			</view>
		</view>
	</view>
</template>

<script>
import { formatDateTime } from '@/utils/dateTimeFormatter.js';
export default {
	data() {
		return {
			activeTab: 'passenger', // 默认激活乘客
			trips: [],
			statusText: {
				0: '进行中',
				1: '已完成',
				2: '已退订'
			},
			page: 1,
			size: 20,
			userInfo: {}
		};
	},
	computed: {},
	onLoad() {
		// 获取乘客行程
		this.getPassengerTrip();
	},
	onShow() {
		this.userInfo = uni.getStorageSync('userInfo');
	},
	methods: {
		// 乘客行程
		async getPassengerTrip() {
			let params = {
				page: this.page,
				size: this.size
				// status: 1 // 默认显示已完成的行程
			};
			try {
				const res = await this.$http.get('/h5/user/trip/passenger', params);
				this.trips = res.data.list || [];
				this.trips &&
					this.trips.forEach((item) => {
						item.start_time = formatDateTime(item.start_time);
					});
			} catch (error) {}
		},
		// 车主行程
		async getMotoristsTrip() {
			let params = {
				page: this.page,
				size: this.size
				// status: 1 // 默认显示已完成的行程
			};
			try {
				const res = await this.$http.get('/h5/user/line/myLine', params);
				this.trips = res.data.list || [];
				this.trips &&
					this.trips.forEach((item) => {
						item.start_time = formatDateTime(item.start_time);
					});
			} catch (error) {}
		},
		// 切换Tab
		switchTab(tab) {
			this.activeTab = tab;
			if (this.activeTab === 'passenger') {
				this.page = 1;
				this.getPassengerTrip();
			} else {
				this.page = 1;
				this.getMotoristsTrip();
			}
		},
		// 导航到指定页面
		navigateTo(data) {
			let url = '';
			if (this.activeTab === 'passenger') {
				url = '/pages/personalCenter/trip/tripDetails?id=' + data.id;
			} else {
				url = '/pages/personalCenter/trip/owners/tripDetails?id=' + data.id;
			}
			uni.navigateTo({
				url: url
			});
		}
	}
};
</script>

<style scoped>
/* 全局样式 */
page {
	background-color: #f5f5f5;
}

.home-container {
	min-height: 100vh;
	background-color: #f5f5f5;
}

/* 顶部导航栏 */
.nav-header {
	background-color: #ffffff;
	padding: 20rpx 0;
	text-align: center;
	border-bottom: 1rpx solid #eee;
}

.nav-title {
	font-size: 36rpx;
	font-weight: 500;
	color: #0b1f20;
}

/* tab样式 */
.tab-bar {
	display: flex;
	/* height: 80rpx; */
	line-height: 80rpx;
	text-align: center;
	background: #ffffff;
	padding: 24rpx 26rpx;
	border-radius: 8rpx;
	overflow: hidden;
}

.tab-item {
	flex: 1;
	font-size: 32rpx;
	color: #666666;
	text-align: center;
	border: 2rpx solid #ff7937;
	border-radius: 0;
	margin: 0;
	transition: all 0.3s ease;
	position: relative;
}

.tab-item:first-child {
	border-top-left-radius: 8rpx;
	border-bottom-left-radius: 8rpx;
	border-right: 1rpx solid #ff7937;
}

.tab-item:last-child {
	border-top-right-radius: 8rpx;
	border-bottom-right-radius: 8rpx;
	border-left: 1rpx solid #ff7937;
}

.tab-item.active {
	background-color: #ff7937;
	color: #ffffff;
	font-weight: 500;
	z-index: 1;
}

.tab-item:not(.active) {
	background-color: #ffffff;
	color: #666666;
}

/* 行程列表样式 */
.trip-list {
	padding: 20rpx 26rpx 34rpx 26rpx;
}

.trip-item {
	background-color: #fff;
	border-radius: 8rpx;
	margin-bottom: 20rpx;
	padding: 30rpx 20rpx;
	border: 1rpx solid #f0f0f0;
}

.trip-item-detail {
	display: flex;
	align-items: center;
}

.departure-info {
	display: flex;
	flex-direction: column;
	flex: 1;
}

.item_info {
	display: flex;
	align-items: center;
	line-height: 1.8;
	font-size: 24rpx;
	color: #666;
}

.item_info:first-child {
	font-weight: bold;
}

.item_info .item_label {
	margin-right: 10rpx;
	margin-left: 10rpx;
}

.action-buttons {
	margin-left: 20rpx;
}

.view-btn {
	background-color: #ffffff;
	color: #333333;
	padding: 8rpx 16rpx;
	border: 1rpx solid #ddd;
	border-radius: 4rpx;
	font-size: 24rpx;
	margin-bottom: 8rpx;
}

.status-text {
	margin-top: 15rpx;
	font-size: 24rpx;
	color: #999999;
}
</style>
